<style>
    #obj-add {
        padding: 20px 25px 25px 0;
    }

    #obj-add .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="obj-add">
    <form class="layui-form" action="" lay-filter="obj-add-form">
        <div class="layui-form-item">
            <label class="layui-form-label">部门：</label>
            <!--<div class="layui-input-block">
                <select id="deptId" name="deptId" lay-verify="required" >
                    <option value=""></option>
                    <option th:each="dept : ${deptList}" th:value="${dept.deptId}" th:text="${dept.deptName }">
                </select>
            </div>-->
            <input type="hidden" id="fileId" name="fileId" class="layui-input" >
            <div class="layui-input-block">
                <input type="text" name="deptId" id="deptId" lay-filter="deptId" class="layui-input dept">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题：</label>
            <div class="layui-input-block">
                <input type="text" id="title" name="title" class="layui-input" lay-verify="required" autocomplete="off" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">政策颁布时间：</label>
            <div class="layui-input-block">
                <input type="text" id="publishTime" lay-verify="required" name="publishTime" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容：</label>
            <div class="layui-input-block">
                <textarea id="content" name="content" style="display: none;"></textarea>
                <!--&lt;!&ndash; 加载编辑器的容器 &ndash;&gt;
                <script id="container" name="content" type="text/plain">
                这里写你的初始化内容
                </script>
-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">附件：</label>
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传文件
            </button>
            <div class="layui-upload-list">
                <p id="demoText"></p>
            </div>
        </div>




        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="obj-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>
<!-- 引入百度编辑器配置文件 -->
<script type="text/javascript" src="/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<!--<script type="text/javascript">
    var ue = UE.getEditor('container',{

        initialFrameHeight: 350, //初始化高度
        autoHeightEnabled: false });
</script>-->
<script data-th-inline="javascript" type="text/javascript">

    layui.use(['febs', 'form', 'formSelects', 'validate', 'treeSelect','layedit','laydate','upload'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            formSelects = layui.formSelects,
            treeSelect = layui.treeSelect,
            form = layui.form,
            upload = layui.upload,
            layedit = layui.layedit,
            laydate = layui.laydate,
            $view = $('#obj-add'),
            validate = layui.validate;




        form.verify(validate);
        form.render();
        //日期时间选择器
        laydate.render({
            elem: '#publishTime'
            ,type: 'datetime'
        });
        treeSelect.render({
            elem: $view.find('#deptId'),
            type: 'get',
            data: ctx + 'dept/select/tree',
            placeholder: '请选择',
            search: false,

        });
        formSelects.render();


        var contentEdit = layedit.build('content',{
            tool: ['strong','italic','underline','del','left', 'center', 'right', 'face'],
        });





        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/tFile/fileUpload' //上传接口
            ,accept: 'file'
            ,multiple:true
            ,size: 0
            ,auto: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demoText').html(file.name); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 200){
                    return layer.msg('上传失败');
                }

                //上传成功
                var fileId = res.data.id;
                var fileName = res.data.realName;
                $("#fileId").val(fileId)
                var demoText = $('#demoText');
                demoText.html(fileName+'<span style="color: green;"><i class="layui-icon layui-icon-ok" style="color: green;"></i>上传成功</span> ');
                return layer.msg('上传成功');
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });




        form.on('submit(obj-add-form-submit)', function (data) {
            //获取编辑器的内容
            var content = layedit.getText(contentEdit)
            data.field.content = content
            febs.post(ctx + 'policy', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增成功');
                $('#febs-obj').find('#query').click();
            });
            return false;
        });
    });
</script>